package com.alibaba.citrus.cr.sales.plan.functions.salesplan;

import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.NotificationConfigQueryRequest;
import com.alibaba.citrus.cr.sales.plan.repository.NotificationConfigRepository;
import com.alibaba.citrus.cr.sales.plan.sdo.NotificationConfigSDO;
import com.epoch.app.bcorder.model.enums.NoticeTitle;
import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.Set;

public class NotificationEntity {
    @Getter
    @Setter
    private NotificationConfigSDO notificationConfigSDO;

    private static NotificationConfigRepository notificationConfigRepository;

    static {
        notificationConfigRepository = SpringContextUtil.getBean(NotificationConfigRepository.class);
    }

    public static NotificationEntity of(NotificationConfigSDO notificationConfigSDO) {
        NotificationEntity entity = new NotificationEntity();
        entity.setNotificationConfigSDO(notificationConfigSDO);
        //this.notificationConfigSDO = notificationConfigSDO;
        return entity;
    }

    public String doCreate() {
        return notificationConfigRepository.save(this.notificationConfigSDO);
    }

    public Boolean doEditRecipient() {
        NotificationConfigQueryRequest notificationConfigQueryRequest = new NotificationConfigQueryRequest();
        notificationConfigQueryRequest.setId(notificationConfigSDO.getId().toString());
        NotificationConfigSDO config = notificationConfigRepository.queryNotificationConfig(notificationConfigQueryRequest);
        Set<String> titls = new HashSet<>();
        titls.add(NoticeTitle.Plan_Adjustment.getValue().toString());
        titls.add(NoticeTitle.Category_Maintenance.getValue().toString());
        if (config != null) {
            if (config.getBusinessUnitCode() == null && !titls.contains(config.getTitle())) {
                throw new FacadeException("OTS-07-005-01-16-008");
            }
        }
        return notificationConfigRepository.doEdit(this.notificationConfigSDO);
    }

    public Boolean doEditContent() {
        return notificationConfigRepository.doEdit(this.notificationConfigSDO);
    }

    public Integer doEditNotificationConfigState() {
        return notificationConfigRepository.editNotificationConfig(this.notificationConfigSDO);
    }
}
